/**
 * @author xialei<home.xialei@gmail.com>
 */
(function(window, $) {
    'use strict';
    // 轮询
    var ajax;
    var app = {
        query: function(callback) {
            ajax && ajax.abort();
            ajax = $.get('/oauth/query', {}, function(data) {
                callback && callback(data);
            }, 'json');
        }
    };


    var timer = setInterval(function() {
        app.query(function(data) {
            if (data.status == 1) {
                timer && clearInterval(timer);

                var $img = $('<img src="' + data.data.avatar + '" class="main-img"/>');
                $(".main").append($img);
                if (!window.oauthed) {
                    var count = window.count+1;
					count = count.toString();
					var len = count.length;
					var html = '';				
					for(var i = 0;i<len;i++){
					 html+='<img  src="/main/img/number/' + count[i] + '.png" height="40" style="margin-left:2px"/>';
					}
                    $(".count span").html(html);
                }
                var point = pointsBig[Math.floor(Math.random() * pointsBig.length)];
                var isLeft = point[0] < 260,
                    isTop = point[1] < 285
                    , left = isLeft ? -Math.random() * 4 : Math.random() * 4
                    , top = isTop ? -Math.random() * 4 : -Math.random() * 4;
                setTimeout(function() {
                    $img.css({
                        transform: 'rotate(360deg)',
                        width: 64,
                        height: 64
                    });
                }, 1000);
                setTimeout(function() {
                    $img.removeClass("main-img").addClass("icon").css({
                        left: point[0] + left,
                        top: point[1] + top,
                        width: 36,
                        height: 36
                    });
                }, 2000);
            }
        });
    }, 3000);


    // 输出坐标
    $('.main').on('click', function(e) {
        var rect = $(this)[0].getBoundingClientRect();
        console.info(e.clientX - rect.left, e.clientY - rect.top);
    });
    var faces = [

    ];
    // 最外层 point
    var pointsBig = [
        [257, 63],
        [242, 74],
        [223, 90],
        [206, 103],
        [189, 120],
        [174, 133],
        [162, 147],
        [144, 165],
        [132, 183],
        [121, 198],
        [115, 214],
        [106, 231],
        [98, 255],
        [91, 286],
        [87, 311],
        [91, 338],
        [99, 370],
        [110, 391],
        [117, 410],
        [128, 426],
        [146, 439],
        [168, 454],
        [187, 465],
        [222, 475],
        [246, 477],
        [271, 482],
        [306, 480],
        [337, 473],
        [360, 460],
        [385, 445],
        [395, 431],
        [406, 410],
        [417, 389],
        [424, 370],
        [430, 348],
        [430, 326],
        [430, 304],
        [431, 271],
        [423, 252],
        [414, 226],
        [405, 207],
        [391, 191],
        [382, 173],
        [367, 155],
        [345, 140],
        [334, 123],
        [317, 111],
        [296, 91],
        [274, 76],
        [257, 65],
        [188, 202],
        [175, 216],
        [161, 243],
        [165, 287],
        [195, 300],
        [219, 296],
        [254, 286],
        [265, 264],
        [257, 224],
        [221, 204],
        [203, 220],
        [217, 250],
        [310, 238],
        [338, 232],
        [369, 242],
        [372, 274],
        [334, 286],
        [319, 272],
        [337, 264],
        [201, 324],
        [228, 339],
        [250, 352],
        [275, 374],
        [281, 397],
        [284, 440],
        [269, 416],
        [291, 362],
        [308, 351],
        [322, 340],
        [342, 327],
        [368, 308],
        [205, 187],
        [253, 197],
        [351, 222]
    ];
    //var count = 500 - window.list.length;
    //// 写50个图片
    //for (var i = 0; i < count; i++) {
    //    var point = pointsBig[Math.floor(Math.random() * pointsBig.length)];
    //    var isLeft = point[0] < 260,
    //        isTop = point[1] < 285
    //        , left = isLeft ? -Math.random() * 20 : -Math.random() * 15
    //        , top = isTop ? -Math.random() * 15 : -Math.random() * 15;
    //    var face = faces[Math.floor(Math.random() * faces.length)];
    //    $('.main').append($('<img src="/main/img/face/' + face + '" class="icon"/>').css({
    //        left: point[0] + left,
    //        top: point[1] + top
    //    }));
    //}
    for (var i = 0; i < window.list.length; i++) {
        var point = pointsBig[Math.floor(Math.random() * pointsBig.length)];
        var isLeft = point[0] < 260,
            isTop = point[1] < 285
            , left = isLeft ? -Math.random() * 2 : Math.random() * 2
            , top = isTop ? -Math.random() * 2 : -Math.random() * 2;
        $('.main').append($('<img src="' + window.list[i].avatar + '" class="icon"/>').css({
            left: point[0] + left,
            top: point[1] + top
        }));
    }
})(window, jQuery);
